<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>猫咖管理系统 - 登录</title>
    <style>
        :root {
            --primary-color: #D85D5D;
            --secondary-color: #9B7E46;
            --neutral-color: #F8F3EA;
            --text-color: #333333;
            --border-color: #DDDDDD;
            --hover-color: #C74D4D;
            --success-color: #4CAF50;
            --error-color: #FF5252;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Inter', system-ui, sans-serif;
        }

        body {
            background: linear-gradient(135deg, var(--neutral-color) 0%, #F9F9F9 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            color: var(--text-color);
            padding: 20px;
        }

        .login-card {
            width: 100%;
            max-width: 400px;
            background-color: white;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
            transition: all 0.3s ease;
            position: relative;
        }

        .login-card:hover {
            box-shadow: 0 15px 40px rgba(0, 0, 0, 0.12);
        }

        .login-header {
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color) 0%, var(--secondary-color) 100%);
        }

        .login-logo {
            text-align: center;
            padding: 32px 24px 24px;
        }

        .login-logo-icon {
            display: inline-block;
            width: 64px;
            height: 64px;
            border-radius: 50%;
            background-color: rgba(216, 93, 93, 0.1);
            margin-bottom: 16px;
            position: relative;
        }

        .login-logo-icon::after {
            content: '';
            position: absolute;
            top: -8px;
            left: -8px;
            width: 80px;
            height: 80px;
            border-radius: 50%;
            border: 1px solid rgba(216, 93, 93, 0.2);
            animation: pulse 2s infinite;
        }

        @keyframes pulse {
            0% { transform: scale(1); opacity: 1; }
            70% { transform: scale(1.1); opacity: 0.5; }
            100% { transform: scale(1); opacity: 1; }
        }

        .login-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 8px;
            color: var(--text-color);
        }

        .login-subtitle {
            font-size: 14px;
            color: #666666;
        }

        .login-form {
            padding: 24px 32px 32px;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-input {
            width: 100%;
            padding: 12px;
            border: 1px solid var(--border-color);
            border-radius: 8px;
            outline: none;
            transition: all 0.3s ease;
            font-size: 16px;
        }

        .form-input:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(216, 93, 93, 0.1);
        }

        .password-toggle {
            position: absolute;
            right: 12px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #999999;
            transition: color 0.3s ease;
            font-size: 14px;
        }

        .password-toggle:hover {
            color: var(--primary-color);
        }

        .form-button {
            width: 100%;
            padding: 12px;
            background: linear-gradient(90deg, var(--primary-color) 0%, var(--hover-color) 100%);
            color: white;
            border: none;
            border-radius: 8px;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.3s ease;
            font-size: 16px;
            box-shadow: 0 4px 12px rgba(216, 93, 93, 0.2);
        }

        .form-button:hover {
            background: linear-gradient(90deg, var(--hover-color) 0%, #B63C3C 100%);
            transform: translateY(-1px);
            box-shadow: 0 6px 16px rgba(216, 93, 93, 0.3);
        }

        .form-button:active {
            transform: translateY(0);
        }

        .form-button:disabled {
            background: #e0e0e0;
            cursor: not-allowed;
            box-shadow: none;
        }

        .login-footer {
            padding: 16px;
            text-align: center;
            font-size: 12px;
            color: #666666;
            border-top: 1px solid #f0f0f0;
        }

        .cat-decoration {
            position: absolute;
            bottom: -30px;
            right: -20px;
            width: 80px;
            height: 80px;
            background-color: var(--primary-color);
            opacity: 0.1;
            border-radius: 50%;
            transform: rotate(15deg);
            z-index: 10;
        }

        @media (max-width: 480px) {
            .login-card {
                margin: 16px;
            }
        }

        .modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5); /* 半透明背景 */
            display: none; /* 默认隐藏 */
            justify-content: center;
            align-items: center;
            z-index: 1000; /* 确保模态框在最顶层 */
        }

        .login-card {
            width: 100%;
            max-width: 400px;
            background-color: white;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
            overflow: hidden;
            transition: all 0.3s ease;
            position: relative;
        }

    </style>
</head>
<body>
<div class="login-card">
    <div class="login-header"></div>

    <div class="login-logo">
        <div class="login-logo-icon"></div>
        <h2 class="login-title">猫咖管理系统</h2>
        <p class="login-subtitle">请登录以继续</p>
    </div>

    <div class="login-form">
        <form id="loginForm">
            <div class="form-group">
                <input type="text" id="username" name="username" class="form-input" placeholder="请输入用户名" required>
            </div>

            <div class="form-group">
                <input type="password" id="password" name="password" class="form-input" placeholder="请输入密码" required>
                <span class="password-toggle" id="togglePassword">
            显示密码
          </span>
            </div>

            <div class="form-group">
                <select id="role" name="role" class="form-input">
                    <option value="user">普通用户</option>
                    <option value="admin">管理员</option>
                </select>
            </div>

            <button type="submit" class="form-button">登录</button>
        </form>
    </div>


    <div class="login-footer">
        <button id="register-btn" class="form-button" style="margin-top: 16px;">注册新用户</button>
        <p>© 2025 猫咖管理系统 版权所有</p>
    </div>

    <div class="cat-decoration"></div>
</div>

<!-- 注册模态框 -->
<div class="modal" id="register-modal" style="display: none;">
    <div class="login-card" style="max-width: 400px; margin: 40px auto;">
        <div class="login-header"></div>

        <div class="login-logo">
            <div class="login-logo-icon"></div>
            <h2 class="login-title">注册账号</h2>
            <p class="login-subtitle">请填写注册信息</p>
        </div>

        <div class="login-form">
            <form id="registerForm">
                <div class="form-group">
                    <input type="text" id="register-username" name="username" class="form-input" placeholder="请输入用户名" required>
                </div>

                <div class="form-group">
                    <input type="password" id="register-password" name="password" class="form-input" placeholder="请输入密码" required>
                    <span class="password-toggle" id="toggleRegisterPassword">显示密码</span>
                </div>

                <div class="form-group">
                    <select id="register-role" name="role" class="form-input">
                        <option value="user">普通用户</option>
                        <option value="admin">管理员</option>
                    </select>
                </div>

                <button type="submit" class="form-button">注册</button>
            </form>
        </div>
    </div>
</div>


<script>
    // 密码显示/隐藏切换
    const togglePassword = document.getElementById('togglePassword');
    const passwordInput = document.getElementById('password');

    togglePassword.addEventListener('click', function() {
        const type = passwordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        passwordInput.setAttribute('type', type);
        togglePassword.textContent = type === 'text' ? '隐藏密码' : '显示密码';
    });

    // 登录表单提交
    const loginForm = document.getElementById('loginForm');

    loginForm.addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取表单数据
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;
        const role = document.getElementById('role').value;

        // 验证用户名格式
        const usernameRegex = /^[a-zA-Z0-9_]{4,16}$/;
        if (!usernameRegex.test(username)) {
            alert('用户名格式不正确，请输入 4 - 16 位字母、数字或下划线');
            return;
        }

        // 准备数据
        const data = {
            username: username,
            password: password,
            role: role
        };

        // 显示加载状态
        const submitButton = loginForm.querySelector('button[type="submit"]');
        submitButton.disabled = true;
        submitButton.textContent = '登录中...';

        // 发送登录请求
        fetch('http://localhost:8080/catcafe/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('登录失败，请检查用户名和密码');
                }
                return response.json();
            })
            .then(result => {
                // 登录成功处理
                console.log('登录成功:', result);

                // 存储从后端返回的用户信息
                if (result.data && result.data.username) {
                    localStorage.setItem('username', result.data.username);
                    localStorage.setItem('role', result.data.role || role);
                    localStorage.setItem('userId',result.data.id)

                    // 显示欢迎信息
                    //alert(`欢迎回来，${result.data.username}！您已成功登录为${result.data.role === 'admin' ? '管理员' : '普通用户'}`);

                    // 跳转到相应页面
                    window.location.href = result.data.role === 'admin' ? 'http://127.0.0.1:8080/index.html' : 'http://127.0.0.1:8080/index.html';
                } else {
                    throw new Error('登录成功但未获取到用户信息');
                }
            })
            .catch(error => {
                // 登录失败处理
                console.error('登录错误:', error);
                alert(error.message || '登录失败，请稍后再试');

                // 恢复按钮状态
                submitButton.disabled = false;
                submitButton.textContent = '登录';
            });
    });

    // 获取注册相关元素
    const registerBtn = document.getElementById('register-btn');
    const registerModal = document.getElementById('register-modal');
    const registerForm = document.getElementById('registerForm');
    const toggleRegisterPassword = document.getElementById('toggleRegisterPassword');
    const registerPasswordInput = document.getElementById('register-password');

    // 显示注册模态框
    registerBtn.addEventListener('click', () => {
        registerModal.style.display = 'block';
    });

    // 隐藏注册模态框
    window.addEventListener('click', (e) => {
        if (e.target === registerModal) {
            registerModal.style.display = 'none';
        }
    });

    // 切换密码显示/隐藏
    toggleRegisterPassword.addEventListener('click', () => {
        const type = registerPasswordInput.getAttribute('type') === 'password' ? 'text' : 'password';
        registerPasswordInput.setAttribute('type', type);
        toggleRegisterPassword.textContent = type === 'text' ? '隐藏密码' : '显示密码';
    });

    // 注册表单提交
    registerForm.addEventListener('submit', function(e) {
        e.preventDefault();

        // 获取表单数据
        const username = document.getElementById('register-username').value;
        const password = document.getElementById('register-password').value;
        const role = document.getElementById('register-role').value;

        // 验证用户名格式
        const usernameRegex = /^[a-zA-Z0-9_]{4,16}$/;
        if (!usernameRegex.test(username)) {
            alert('用户名格式不正确，请输入 4 - 16 位字母、数字或下划线');
            return;
        }

        if (password.length < 6) {
            alert('密码长度不能少于6位');
            return;
        }

        // 准备请求体
        const data = {
            username,
            password,
            role
        };

        // 显示加载状态
        const submitButton = registerForm.querySelector('button[type="submit"]');
        submitButton.disabled = true;
        submitButton.textContent = '注册中...';

        // 发送注册请求
        fetch('http://localhost:8080/catcafe/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
            .then(response => {
                if (!response.ok) {
                    throw new Error('注册失败，请检查网络或重试');
                }
                return response.json();
            })
            .then(result => {
                alert('注册成功！您可以使用新账户登录了。');
                registerModal.style.display = 'none'; // 关闭模态框
                submitButton.disabled = false;
                submitButton.textContent = '注册';
            })
            .catch(error => {
                console.error('注册错误:', error);
                alert(error.message || '注册失败，请稍后再试');
                submitButton.disabled = false;
                submitButton.textContent = '注册';
            });
    });

    // 隐藏注册模态框
    window.addEventListener('click', (e) => {
        if (e.target === registerModal) {
            registerModal.style.display = 'none';
        }
    });

</script>
</body>
</html>